package com.iman.manjusri.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import com.iman.common.util.ConfigUtil;
import com.iman.manjusri.domain.SessionBean;

/**
 * you can use this initBinder() to bind string to date , or
 * you can user @DateTimeFormat(style="S-", iso=DateTimeFormat.ISO.DATE) annoation to bind string to date.
 */
public abstract class BaseController {
    public static final String defaultDateFormat = "yyyy-MM-dd";

   
    /*
	    login.agent=agent.520xinli.com
		login.student=student.520xinli.com
		login.teacher=teacher.520xinli.com
		login.main=www.520xinli.com
		login.space=space.520xinli.com
    */
  protected  String forward_agent  		=	ConfigUtil.getString( "login.agent" );; 
  protected  String forward_student  	=	ConfigUtil.getString( "login.student" );; 
  protected  String forward_teacher  	=	ConfigUtil.getString( "login.teacher" );; 
  protected  String forward_main  		=	ConfigUtil.getString( "login.main" );; 
  protected  String forward_space  		=	ConfigUtil.getString( "login.space" );; 
    
    
	
	public  void putResourceSrc(ModelAndView view){
		 //图片的域名  	   
		 String imgbasePath  = ConfigUtil.getString( "image.server" );
		 //css 服务器连接    在 context.properties 配置
		 String cssbasePath  = ConfigUtil.getString( "css.server" );
		 //css 服务器连接    在 context.properties 配置
		 String jsbasePath  = ConfigUtil.getString( "js.server" );
		 // 图片访问
		 String drmFilePath = ConfigUtil.getString("drmFile.server");
		 
		 // 图片访问
		 String cdrmFilePath = ConfigUtil.getString("cdrmFile.server");
		  
		 String videoPath = ConfigUtil.getString("video.server");
			 
	     view.addObject("videoPath", videoPath);
		 view.addObject("drmFilePath", drmFilePath);
		 view.addObject("cdrmFilePath", cdrmFilePath);
		 
		 view.addObject("imgpath", imgbasePath);
		 view.addObject("csspath", cssbasePath);
		 view.addObject("jspath", jsbasePath);
		 
		 view.getModel().put("jsbasePath", jsbasePath );
	 
	}
	
	 
  
    
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(getDateFormat());
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    //如果有需求：日期格式不是yyyy-MM-dd,则需要覆盖这个方法。
    public String getDateFormat() {
        return defaultDateFormat;
    }
    
    
    
	/**
	 * 
	 *  Function:
	 *  功能说明： 获取session
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2014年12月18日 下午12:24:48
	 *	返回类型: SessionBean    
	 *  @param request
	 *  @return
	 */
	public SessionBean getSession(HttpServletRequest request){
		HttpSession session = request.getSession();
		SessionBean	 sessionBean = (SessionBean)session.getAttribute(SessionBean.USER_SESSION_KEY_PERFIX+SessionBean.USER_SESSION_SUBKEY_SESSION );
		return sessionBean;
	}
	
	
	/**
	 * 
	 *  Function:
	 *  功能说明： 获取session  获取sessionid  用户ID  
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2014年12月18日 下午12:24:48
	 *	返回类型: SessionBean    
	 *  @param request
	 *  @return
	 */
	public Long getUserIdInSession(HttpServletRequest request){
		 HttpSession session = request.getSession();
		SessionBean	 sessionBean = (SessionBean)session.getAttribute(SessionBean.USER_SESSION_KEY_PERFIX+SessionBean.USER_SESSION_SUBKEY_SESSION );
		if(sessionBean !=null && sessionBean.getUser()!=null ){
			return sessionBean.getUser().getUserId();
		}
		
		return null; 
	}
	
}
